package com.qf.java2105.huangchuang.hotel.controller.foodtype;

import com.alibaba.druid.util.StringUtils;
import com.qf.java2105.huangchuang.hotel.constant.MessageConstant;
import com.qf.java2105.huangchuang.hotel.constant.ResponseMessageConstant;
import com.qf.java2105.huangchuang.hotel.controller.BaseServlet;
import com.qf.java2105.huangchuang.hotel.entity.ResultVO;
import com.qf.java2105.huangchuang.hotel.pojo.FoodType;
import com.qf.java2105.huangchuang.hotel.utils.JsonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author hcvg
 * @version v1.0
 * @date 2021/09/13
 **/
@WebServlet("/foodtype")
public class FoodTypeController extends BaseServlet {

    /**
     * 菜系通用查询
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    private String search(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String keyword = request.getParameter("keyword");
        if (StringUtils.isEmpty(keyword)){
            keyword = null;
        }else {
            keyword = keyword.trim();
        }
        ResultVO<List<FoodType>> resultVO = FOOD_TYPE_SERVICE.findByFoodTypeName(keyword);
        // response.getWriter().write(JsonUtils.objectToJson(resultVO));
        request.setAttribute("keyword", keyword);
        request.setAttribute("foodtypes", resultVO.getData());
        // request.getRequestDispatcher(request.getContextPath() + "/backend/detail/foodtype/foodtype-list.jsp").forward(request, response);
        return ResponseMessageConstant.PREFIX_FORWARD + request.getContextPath() + "/backend/detail/foodtype/foodtype-list.jsp";
    }

    /**
     * 菜系更新页面
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    private String updateui(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String typeId = request.getParameter("typeId");
        if (StringUtils.isEmpty(typeId)){
            return "<script>alert('" + MessageConstant.TYPE_ID_ERROR + "'); location.href = \"/foodtype?method=search\"</script>";
        }else {
            typeId = typeId.trim();
        }
        ResultVO<FoodType> resultVO = FOOD_TYPE_SERVICE.findFoodTypeById(Integer.valueOf(typeId));
        request.setAttribute("foodType", resultVO.getData());
        return ResponseMessageConstant.PREFIX_FORWARD + request.getContextPath() + "/backend/detail/foodtype/foodtype-update.jsp";
    }

    /**
     * 菜系更新
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    private String update(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String foodTypeId = request.getParameter("foodTypeId");
        String foodTypeName = request.getParameter("foodTypeName");
        if (!StringUtils.isEmpty(foodTypeId) && !StringUtils.isEmpty(foodTypeName)){
            ResultVO<String> resultVO = FOOD_TYPE_SERVICE.update(Integer.valueOf(foodTypeId), foodTypeName);
            if (resultVO.getSuccess()){
                return ResponseMessageConstant.PREFIX_REDIRECT + request.getContextPath() + "/foodtype?method=search";
            }
            return "<script>alert('" + resultVO.getMessage() + "'); location.href = \"/foodtype?method=search\"</script>";
        }else {
            //return "<script>alert('" + MessageConstant.NULL_FOOD_TYPE_NAME + "');</script>";
            return "<script>alert('" + MessageConstant.NULL_FOOD_TYPE_NAME + "'); location.href = \"/foodtype?method=search\"</script>";
        }

    }

    /**
     * 菜系删除
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    private String delete(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String typeId = request.getParameter("typeId");
        if (StringUtils.isEmpty(typeId)){
            return "<script>alert('" + MessageConstant.TYPE_ID_ERROR + "'); location.href = \"/foodtype?method=search\"</script>";
        }else {
            typeId = typeId.trim();
        }
        ResultVO<String> resultVO = FOOD_TYPE_SERVICE.delete(Integer.valueOf(typeId));
        if (resultVO.getSuccess()){
            return "<script>alert('" + resultVO.getMessage() + "'); location.href = \"/foodtype?method=search\"</script>";
        }
        return "<script>alert('" + ResponseMessageConstant.CAN_NOT_DELETE_FOOD_TYPE_WITH_FOOD + "'); location.href = \"/foodtype?method=search\"</script>";
    }

    /**
     * 菜系添加
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    private String save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String typeName = request.getParameter("typeName");
        if (StringUtils.isEmpty(typeName)){
            return "<script>alert('" + MessageConstant.NULL_FOOD_TYPE_NAME + "'); location.href = \"/foodtype?method=search\"</script>";
        }
        ResultVO<String> resultVO = FOOD_TYPE_SERVICE.save(typeName);
        if (!resultVO.getSuccess()){
            return "<script>alert('" + resultVO.getMessage() + "'); location.href = \"/foodtype?method=search\"</script>";
        }
        return ResponseMessageConstant.PREFIX_REDIRECT + request.getContextPath() + "/foodtype?method=search";

    }

    /**
     * 跳转菜系添加页面
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    private String saveui(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        return ResponseMessageConstant.PREFIX_FORWARD + request.getContextPath() + "/backend/detail/foodtype/foodtype-save.jsp";
    }

    /**
     * 异步验证菜系是否存在
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    private String foodTypeExist(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String typeName = request.getParameter("typeName");
        ResultVO<String> resultVO = FOOD_TYPE_SERVICE.foodTypeExist(typeName);
        return JsonUtils.objectToJson(resultVO);
    }

    /**
     * 前台查询所有菜系并放入session中
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    private String frontSearch (HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        HttpSession session = request.getSession();
        // 从session中获取菜系数据
        List<FoodType> frontTypes = (List<FoodType>) session.getAttribute("front_types");
        // 没有则从数据库中查询后放到session中
        if (null == frontTypes || frontTypes.size() == 0){
            ResultVO<List<FoodType>> resultVO = FOOD_TYPE_SERVICE.findByFoodTypeName(null);
            session.setAttribute("front_types", resultVO.getData());
            return JsonUtils.objectToJson(resultVO);
        }
        // 有就返回session中的菜系数据
        return JsonUtils.objectToJson(ResultVO.ok(ResponseMessageConstant.SESSION_EXIST_FOOD_TYPE, frontTypes));
    }
}
